package com.sun.bata;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLlite extends SQLiteOpenHelper {
	public static final int VERSION = 1;

	private String tabName = "doublelotter";
	private SQLiteDatabase 	db;
	public SQLlite(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public SQLlite(Context context) {
		this(context, "lotter_db");
	}

	public SQLlite(Context context, String name) {
		this(context, name, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table "
				+ tabName
				+ "(id int,time varchar(20),lotterName varchar(20),lotterplay varchar(20),lotterNumbers varchar(20))");
	}

	/**
	 * 鎻掑叆鏁版嵁
	 * 
	 * @param values
	 */
	public void insert(String tabName, ContentValues values) {
		this.tabName = tabName;
		db = getWritableDatabase();
		// 鎵撳紑鏁版嵁搴�
		onOpen(db);
		// 鎻掑叆 values
		db.insert(tabName, null, values);
		// 鍏抽棴 SQLiteDataBase
		db.close();
	}

	public Cursor query(String tabName) {
		this.tabName = tabName;
		// 鑾峰彇 SQLiteDataBase 瀹炰緥;
		// 鎵撳紑涓�釜浠呰鏁版嵁搴�
		db = getReadableDatabase();
		onOpen(db);
		Cursor c = db.query(tabName, new String[] { "time", "lotterName", "lotterplay","lotterNumbers" },
				"id=?", new String[] { "1" }, null, null, null);
		c.getCount();
		// 鍏抽棴鏁版嵁搴�
		db.close();
		return c;
	}
	public Cursor rawQuery(String tabName){
		db = getReadableDatabase();
		return db.rawQuery("select * from " +tabName, null); 
	}
	
	public void deleteRaw(String tabName,int raw){
			db = getWritableDatabase();  
			onOpen(db);
            db.execSQL("delete from " + tabName + " where id = "+raw); 
            String sql = "";
            Cursor c = rawQuery(tabName);
            c.move(raw); 
            raw ++;
           
            while (c.moveToNext()) {
            	sql = "Update "+tabName+" set id = "+(raw-1)+" where id = "+raw;
                db.execSQL(sql);
				raw++;
			}
            db.close();
            
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

}
